/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.jalbum.instantpreview;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import se.datadosen.jalbum.AlbumBean;
import se.datadosen.jalbum.JAlbumFrame;
import se.datadosen.jalbum.JAlbumPlugin;
import se.datadosen.jalbum.PluginContext;
import se.datadosen.util.Stopwatch;


/**
 *
 * @author david
 */
public class InstantPreviewPlugin implements JAlbumPlugin {

    PluginContext context;

    Action instantPreviewAction = new AbstractAction("Instant preview") {
        {
            putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
        }
        public void actionPerformed(ActionEvent e) {
            final JAlbumFrame window = context.getJAlbumContext().getFrame();
            String msg = "Generating preview...";
            window.statusBar.pushText(msg);
            AlbumBean engine = new AlbumBean();
            window.ui2Engine(engine);
            try { 
                engine.setSubdirs(false);
                //engine.setSlides(false);
                engine.setAppendImages(true);
                int imagesPerPage = engine.getCols() * engine.getRows();
                if (imagesPerPage == 0 || imagesPerPage > 25) {
                    imagesPerPage = engine.getCols() * 3;
                }
                engine.setLimit(imagesPerPage);
                engine.makeAlbum();
                window.statusBar.popText(msg);
                window.actions.viewAlbumAction.actionPerformed(e);
            }
            catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }

    };

    public void init(PluginContext context) {
        this.context = context;
        context.addToolsMenuItem(new JMenuItem(instantPreviewAction));
    }

    public boolean onExit() {
        return true;
    }

}
